﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETPerformance.App_BusinessLogic.BasePages;
using ASPNETPerformance.App_BusinessLogic.Data;
using ASPNETPerformance.App_BusinessLogic.Extensions;
using ASPNETPerformance.App_BusinessLogic.SEO;

namespace ASPNETPerformance._08_AjaxAndJson {
    public partial class Tmpl : BasePage {
        protected void Page_Load( object sender, EventArgs e ) {

        }

        protected override void OnPreRender( EventArgs e ) {
            base.OnPreRender( e );

            SEOHelper.AddJavaScriptInclude( this, "~/_assets/js/jquery.tmpl.min.js" );
        }

        protected void DrpCategories_SelectedIndexChanged( object sender, EventArgs e ) {
            int categoryID = 0;
            if ( int.TryParse( this.drpCategories.SelectedValue, out categoryID ) && categoryID > 0 ) {
                using ( var dbContext = new NORTHWNDEntities() ) {
                    var products = from product in dbContext.Products
                                   where product.CategoryID == categoryID
                                   orderby product.ProductName
                                   select new {
                                       product.ProductName,
                                       product.QuantityPerUnit,
                                       product.UnitPrice,
                                       product.Discontinued
                                   };


                    this.rptProducts.DataSource = products.ToList();
                    this.rptProducts.DataBind();
                }
            }
        }
    }
}